package com.example.controller;

import com.example.dao.ShenqingDao;
import com.example.pojo.Shenqing;
import com.example.util.FreeMarkUtils;
import com.example.util.ResultUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.io.*;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;

@Controller
public class FreemarkerController {

    @Resource
    ShenqingDao shenqingDao;

    private static String document= "document.xml";

    /**
     * 导出申请表
     *
     * @param id
     * @return
     */
    @RequestMapping("manage/dc")
    @ResponseBody
    public ResultUtil dc(Integer id) {
        try {
            Shenqing shenqing = shenqingDao.queryById(id);
            System.out.println(shenqing);
            Map dataMap=new HashMap();
            dataMap.put("xueyuan", shenqing.getXueyuan());
            dataMap.put("zhuanye", shenqing.getZhuanye());
            dataMap.put("classname", shenqing.getClassname());
            dataMap.put("name", shenqing.getName());
            dataMap.put("gender", shenqing.getGender());
            dataMap.put("birthday", shenqing.getBirthday());
            dataMap.put("jiguan", shenqing.getJiguan());
            dataMap.put("idnum", shenqing.getIdnum());
            dataMap.put("familynum", shenqing.getFamilynum());
            dataMap.put("tel", shenqing.getTel());
            dataMap.put("addr", shenqing.getAddr());
            dataMap.put("youbian", shenqing.getYoubian());
            dataMap.put("familytel", shenqing.getFamilytel());
            dataMap.put("Intro", shenqing.getIntro());
            dataMap.put("yyijian", shenqing.getYyijian());
            dataMap.put("yijian", shenqing.getXyijian());


            //指定输出docx路径
//            C:\Users\Administrator\Desktop\zizhu\zizhu\src\main\resources\templates
            File outFile = new File("C:\\Users\\Administrator\\Desktop\\项目资料\\zizhu\\zizhu\\src\\main\\resources\\templates\\家庭经济困难学生认定申请表.docx") ;
            createDocx(dataMap,new FileOutputStream(outFile));

            return ResultUtil.ok("导出申请表成功");
        } catch (Exception e) {
            return ResultUtil.error("导出申请表,稍后再试！");
        }
    }



    //outputStream 输出流可以自己定义 浏览器或者文件输出流
    public static void createDocx(Map dataMap, OutputStream outputStream) {
        ZipOutputStream zipout = null;
        try {
            /*//图片配置文件模板
            ByteArrayInputStream documentXmlRelsInput = FreeMarkUtils.getFreemarkerContentInputStream(dataMap, documentXmlRels);*/

            //内容模板
            ByteArrayInputStream documentInput = FreeMarkUtils.getFreemarkerContentInputStream(dataMap, document);
            //最初设计的模板
            //File docxFile = new File(WordUtils.class.getClassLoader().getResource(template).getPath());
            File docxFile = new File("C:\\Users\\Administrator\\Desktop\\项目资料\\zizhu\\zizhu\\src\\main\\resources\\templates\\家庭经济困难学生认定申请表(模版).zip");//换成自己的zip路径
            if (!docxFile.exists()) {
                System.out.println(111111);
                docxFile.createNewFile();
            }
            ZipFile zipFile = new ZipFile(docxFile);
            Enumeration<? extends ZipEntry> zipEntrys = zipFile.entries();
            zipout = new ZipOutputStream(outputStream);
            //开始覆盖文档------------------
            int len = -1;
            byte[] buffer = new byte[1024];
            while (zipEntrys.hasMoreElements()) {
                ZipEntry next = zipEntrys.nextElement();
                InputStream is = zipFile.getInputStream(next);
                if (next.toString().indexOf("media") < 0) {
                    zipout.putNextEntry(new ZipEntry(next.getName()));
                    if ("word/document.xml".equals(next.getName())) {//如果是word/document.xml由我们输入
                        if (documentInput != null) {
                            while ((len = documentInput.read(buffer)) != -1) {
                                zipout.write(buffer, 0, len);
                            }
                            documentInput.close();
                        }
                    } else {
                        while ((len = is.read(buffer)) != -1) {
                            zipout.write(buffer, 0, len);
                        }
                        is.close();
                    }
                }
            }

        } catch (Exception e) {
            System.out.println("word导出失败:"+e.getStackTrace());
            //logger.error();
        }finally {
            if(zipout!=null){
                try {
                    zipout.close();
                } catch (IOException e) {
                    System.out.println("io异常");

                }
            }
            if(outputStream!=null){
                try {
                    outputStream.close();
                } catch (IOException e) {
                    System.out.println("io异常");
                }
            }
        }
    }
}
